<template>
  <div>
    <el-container class="home-container">
      <!-- 头部区 -->
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="" />
          <span>电商管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体区 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollpase ? '64px' : '200px'">
          <el-menu
            background-color="#458b74"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollpase"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <div class="toggle-button" @click="toggleCollapse">
              <i
                :class="isCollpase ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
              ></i>
            </div>
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menulist"
              :key="item.id"
            >
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <!-- 一级菜单的图标 -->
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState(subItem.path)"
              >
                <template slot="title">
                  <!-- 二级菜单的图标 -->
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容区 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单栏的所有数据
      menulist: [],
      //一级菜单图标
      iconsObj: {
        125: "el-icon-user-solid",
        103: "el-icon-view",
        101: "el-icon-goods",
        102: "el-icon-s-order",
        145: "el-icon-s-data",
      },
      //是否折叠菜单栏
      isCollpase: false,
      //被激活的链接地址
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      //清空会话缓存中的token值
      window.sessionStorage.clear();
      //路由跳转到登录页
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      //判断网络数据是否请求成功，若失败返回msg，若成功把请求到的数据data赋值给menulist
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      } else {
        this.menulist = res.data;
      }
      //console.log(res);
    },
    //点击点击按钮切换菜单的关闭和展开
    toggleCollapse() {
      this.isCollpase = !this.isCollpase;
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
div {
  height: 100%;
  margin: 0;
}
.home-container {
  height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  background-color: #483d8b;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #458b74;
  text-align: center;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #ebebeb;
  text-align: center;
}

.toggle-button {
  background-color: #483d8b;
  font-size: 24px;
  text-align: center;
  color: #fff;
  cursor: pointer;
  line-height: 8px;
  border-top: 1px solid #458b74;
}
</style>
